package com.wlc.business.util.http;

/**
 * Created by jiyi on 14/7/8.
 */

import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
//用于进行Https请求的HttpClient
//@SuppressWarnings("deprecation")

public class SSLClient /* extends DefaultHttpClient */{

	/*
	 * public SSLClient() throws Exception{ super(); SSLContext ctx =
	 * SSLContext.getInstance("TLS"); X509TrustManager tm = new
	 * X509TrustManager() {
	 * 
	 * @Override public void checkClientTrusted(X509Certificate[] chain, String
	 * authType) throws CertificateException { }
	 * 
	 * @Override public void checkServerTrusted(X509Certificate[] chain, String
	 * authType) throws CertificateException { }
	 * 
	 * @Override public X509Certificate[] getAcceptedIssuers() { return null; }
	 * }; ctx.init(null, new TrustManager[]{tm}, null); SSLSocketFactory ssf =
	 * new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
	 * ClientConnectionManager ccm = this.getConnectionManager(); SchemeRegistry
	 * sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf));
	 * }
	 */

	/*
	 * public CloseableHttpClient getIgnoeSSLClient() throws Exception {
	 * SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new
	 * TrustStrategy() {
	 * 
	 * @Override public boolean isTrusted(X509Certificate[] x509Certificates,
	 * String s) throws CertificateException { return true; } }).build();
	 * 
	 * //创建httpClient CloseableHttpClient client =
	 * HttpClients.custom().setSSLContext(sslContext).
	 * setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); return
	 * client; }
	 */
	private static final String HTTP = "http";
	private static final String HTTPS = "https";
	private static PoolingHttpClientConnectionManager cm = null;
	private static SSLConnectionSocketFactory sslsf = null;
	private static SSLContextBuilder builder = null;

	static {
		try {
			builder = new SSLContextBuilder();
			// 全部信任 不做身份鉴定
			builder.loadTrustMaterial(null, new TrustStrategy() {
				@Override
				public boolean isTrusted(X509Certificate[] x509Certificates,
						String s) throws CertificateException {
					return true;
				}
			});
			sslsf = new SSLConnectionSocketFactory(builder.build(),
					new String[] { "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2" },
					null, NoopHostnameVerifier.INSTANCE);
			Registry<ConnectionSocketFactory> registry = RegistryBuilder
					.<ConnectionSocketFactory> create()
					.register(HTTP, new PlainConnectionSocketFactory())
					.register(HTTPS, sslsf).build();
			cm = new PoolingHttpClientConnectionManager(registry);
			cm.setMaxTotal(200);// max connection
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static CloseableHttpClient getHttpClient() throws Exception {
		CloseableHttpClient httpClient = HttpClients.custom()
				.setSSLSocketFactory(sslsf).setConnectionManager(cm)
				.setConnectionManagerShared(true).build();
		return httpClient;
	}

}
